package com.gadgets.busi.bm08.service;

import com.gadgets.busi.bm08.common.Bm08Constant;
import com.gadgets.busi.bm08.common.dto.QuestionGroupDTO;
import com.gadgets.busi.bm08.common.dto.QuestionQueryDTO;
import com.gadgets.busi.bm08.common.dto.QuestionResultDTO;
import com.gadgets.busi.bm08.common.dto.QuestionTotalDTO;
import com.gadgets.busi.bm08.pojo.HdsQstSelection;
import com.gadgets.busi.bm08.pojo.HdsQuestion;
import com.gadgets.busi.bm08.repository.IHdsQstSelectRepository;
import com.gadgets.busi.bm08.repository.IHdsQustionRepository;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能说明：
 * 软件版权：恒生电子股份有限公司
 *
 * @author xiujunqing
 * @version V202101.00.000
 * @data 2022-04-19
 * 修改记录：  修改日期          修改人员    修改说明
 * 2022-04-19  xiujunqing   创建文件
 */
@Service
@Transactional(rollbackFor = Throwable.class, transactionManager = "gadgetsDbTransactionManage")
public class QuestionManageService implements IQuestionManageService{

    @Autowired
    private IHdsQustionRepository qustionRepository;
    @Autowired
    private IHdsQstSelectRepository qstSelectRepository;

    /**
     * 保存题目
     *
     * @param question      题目信息
     * @param selectionList 选项/答案信息
     * @throws GadgetsServiceException 异常信息
     */
    @Transactional(rollbackFor = Throwable.class, transactionManager = "gadgetsDbTransactionManage")
    @Override
    public void saveQuestion(HdsQuestion question, List<HdsQstSelection> selectionList) throws GadgetsServiceException {
        calculateQuestionPoint(question, selectionList);
        // 保存题目
        qustionRepository.insertQuestion(question);
        // 设置选项ID，保存选项
        for (HdsQstSelection hdsQstSelection : selectionList) {
            hdsQstSelection.setQuestionId(question.getId());
            qstSelectRepository.insertQuestionSelect(hdsQstSelection);
        }
    }

    /**
     * 计算题目分值
     * @param question          题目信息
     * @param selectionList     选项列表
     */
    protected void calculateQuestionPoint(HdsQuestion question, List<HdsQstSelection> selectionList) {
        if(Bm08Constant.QUEST_TYPE.COME_IN_BLANK.equals(question.getType())) {
            question.setPoint(selectionList.size());
        }else{
            question.setPoint(Bm08Constant.QUEST_POINT_DICT.valueOf("T"+question.getType()).getPoint());
        }
    }

    /**
     * 查询全部题目ID
     *
     * @param queryDTO 查询条件
     * @return 查询结果
     * @throws GadgetsServiceException 异常信息
     */
    @Override
    public List<Long> selectQuestionIdList(QuestionQueryDTO queryDTO) throws GadgetsServiceException {
        queryDTO.addAsc("MODUEL");
        queryDTO.addAsc("ID");
        return qustionRepository.selectQuestionIdByCondition(queryDTO);
    }

    /**
     * 根据ID查询题目信息
     *
     * @param id 题目ID
     * @return 查询结果
     * @throws GadgetsServiceException 异常信息
     */
    @Override
    public QuestionResultDTO selectQuestionResultById(Long id) throws GadgetsServiceException {
        List<QuestionResultDTO> resultDTOList = qustionRepository.selectQstInfoByCondition(id);
        if(null!=resultDTOList && resultDTOList.size()>0){
            QuestionResultDTO resultDTO = resultDTOList.get(0);
            // 查询选项信息
            resultDTO.setSelections(getQstSelectRepository().selectQstSelectByQstId(id));
            return resultDTO;
        }
        return null;
    }

    /**
     * 查询题目汇总信息
     *
     * @return 汇总信息
     *
     * @throws GadgetsServiceException 异常信息
     */
    @Override
    public List<QuestionTotalDTO> selectQuestionTotal() throws GadgetsServiceException {
        return getQustionRepository().moduelQstTotal();
    }

    /**
     * 分组统计题目信息
     * @return 分组信息
     * @throws GadgetsServiceException 异常信息
     */
    @Override
    public List<QuestionGroupDTO> selectQstGroupingInfo() throws GadgetsServiceException {
        return getQustionRepository().selectQstGroupingInfo();
    }

    /**
     * 根据题目题型查询题目列表
     * @param type 题目类型
     * @return 查询结果
     * @throws GadgetsServiceException 异常信息
     */
    @Override
    public List<HdsQuestion> selectQstGenerateInfoByType(String type) throws GadgetsServiceException {
        List<HdsQuestion> resultList = qustionRepository.selectQstGenerateInfoByType(type);
        return resultList;
    }

    public IHdsQustionRepository getQustionRepository() {
        return qustionRepository;
    }

    public void setQustionRepository(IHdsQustionRepository qustionRepository) {
        this.qustionRepository = qustionRepository;
    }

    public IHdsQstSelectRepository getQstSelectRepository() {
        return qstSelectRepository;
    }

    public void setQstSelectRepository(IHdsQstSelectRepository qstSelectRepository) {
        this.qstSelectRepository = qstSelectRepository;
    }
}
